<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="订单时间" prop="createTimeDateRange">
        <el-date-picker
          v-model="createTimeDateRange"
          size="small"
          style="width: 240px"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="支付时间" prop="payTimeDateRange">
        <el-date-picker
          v-model="payTimeDateRange"
          size="small"
          style="width: 240px"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="订单状态" prop="payStatus">
        <el-select v-model="queryParams.payStatus" placeholder="请选择订单状态" clearable size="small">
          <el-option label="未支付" value="0" />
          <el-option label="支付成功" value="1" />
          <el-option label="支付失败" value="2" />
          <el-option label="已退款" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单号" prop="outTradeNo">
        <el-input
          v-model="queryParams.outTradeNo"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入用户手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['common:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键ID" align="center" prop="id" /> -->
      <el-table-column label="订单号" align="center" prop="outTradeNo" />
      <el-table-column label="支付宝交易号" align="center" prop="tradeNo" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户手机号" align="center" prop="phone" />
      <el-table-column label="订单标题" align="center" prop="subject" />
      <el-table-column label="实际支付金额" align="center" prop="receiptAmount" />
      <el-table-column label="充值金额" align="center" prop="costPrice" />
      <el-table-column label="利润" align="center" prop="profit" />
      <el-table-column label="充值账号" align="center" prop="remark" />
      <el-table-column label="订单状态" align="center" prop="payStatusName" />
      <el-table-column label="充值状态" align="center" prop="rechargeStatusName" />
      <el-table-column label="订单时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.payStatus==1"
            size="mini"
            type="text"
            @click="updateOrderChange(scope.row)"
            v-hasPermi="['common:order:edit']"
          >退款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listOrder, getOrder, updateOrder, exportOrder } from "@/api/business/rechargeOrder/order";

export default {
  name: "rechargeOrder",
  data() {
    return {
      createTimeDateRange: [new Date(),new Date()],
      payTimeDateRange: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付宝订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        outTradeNo: null,
        tradeNo: null,
        subject: null,
        body: null,
        totalAmount: null,
        receiptAmount: null,
        buyerId: null,
        buyerLogonId: null,
        sellerId: null,
        tradeStatus: null,
        payStatus: null,
        appId: null,
        userId: null,
        productId: null,
        goodsType: null,
        timeoutExpress: null,
        notifyUrl: null,
        returnUrl: null,
        passbackParams: null,
        payTime: null,
        closeTime: null,
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatDate(date,type) {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    /** 查询支付宝订单列表 */
    getList() {
      if(this.createTimeDateRange && this.createTimeDateRange.length > 0){
        this.queryParams.beginCreateTime  = this.formatDate(this.createTimeDateRange[0]);
        this.queryParams.endCreateTime = this.formatDate(this.createTimeDateRange[1]);
      }else{
        this.queryParams.beginCreateTime  = null;
        this.queryParams.endCreateTime = null;
      }
      if(this.payTimeDateRange && this.payTimeDateRange.length > 0){
        this.queryParams.beginPayTime = this.formatDate(this.payTimeDateRange[0]);
        this.queryParams.endPayTime = this.formatDate(this.payTimeDateRange[1]);
      }else{
        this.queryParams.beginPayTime = null;
        this.queryParams.endPayTime = null;
      }
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createTimeDateRange = [new Date(),new Date()];
      this.payTimeDateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有支付宝订单数据项？').then(() => {
        this.exportLoading = true;
        return exportOrder(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    updateOrderChange(row){
      this.$modal.confirm('是否确认进行退款？').then(() => {
        return updateOrder(row);
      }).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      }).catch(() => {});
    }
  }
};
</script>
